package config

import (
	"encoding/json"
	"fmt"
	"gitee.com/pearl-zz/go-manage-system-core/config/source/file"
	"gitee.com/pearl-zz/go-manage-system-core/sdk/config"
	"github.com/spf13/cobra"
)

var (
	configYml string
	StartCmd  = &cobra.Command{
		Use:     "config",
		Short:   "Get Application config info",
		Example: "go-manage-system config -c config/settings.yml",
		Run: func(cmd *cobra.Command, args []string) {
			run()
		},
	}
)

func init() {
	StartCmd.PersistentFlags().StringVarP(&configYml, "config", "c", "config/settings.yml", "Start server with provided configuration file")
}

func run() {
	config.Setup(file.NewSource(file.WithPath(configYml)))

	application, err := json.MarshalIndent(config.ApplicationConfig, "", "   ") // 转换成JSON返回的是byte[]
	if err != nil {
		fmt.Println(err.Error())
	}
	fmt.Println("application:", string(application))

	jwt, err := json.MarshalIndent(config.JwtConfig, "", "   ") // 转换成JSON返回的是byte[]
	if err != nil {
		fmt.Println(err.Error())
	}
	fmt.Println("jwt:", string(jwt))

	database, err := json.MarshalIndent(config.DatabasesConfig, "", "   ") // 转换成JSON返回的是byte[]
	if err != nil {
		fmt.Println(err.Error())
	}
	fmt.Println("database:", string(database))

	gen, err := json.MarshalIndent(config.GenConfig, "", "   ")
	if err != nil {
		fmt.Println(err.Error())
	}
	fmt.Println("gen:", string(gen))

	loggerConfig, err := json.MarshalIndent(config.LoggerConfig, "", "   ")
	if err != nil {
		fmt.Println(err.Error())
	}
	fmt.Println("logger:", string(loggerConfig))
}
